﻿<?php
require_once dirname(__FILE__).'\ConnectionDB.php';
class DataBasePDO
{
	private $_connectDb;
	static $_instance;
	private $SQL_SERVER = Array('host'=>'127.0.0.1', 
							'port'=>'3306', 
							'dbname'=>'mysql', 
							'user'=>'root', 
							'password'=>'kaida');
	
	private function __construct() {
		$this->_connectDb = $this->connectDb();
	}
	public function resetServer($sql_server = Array())
	{
		if(!empty($sql_server))
		{
			$this->SQL_SERVER['host'] = isset($sql_server['host']) ? $sql_server['host'] : $this->SQL_SERVER['host'];
			$this->SQL_SERVER['port'] = isset($sql_server['port']) ? $sql_server['port'] : $this->SQL_SERVER['port'];
			$this->SQL_SERVER['dbname'] = isset($sql_server['dbname']) ? $sql_server['dbname'] : $this->SQL_SERVER['dbname'];
			$this->SQL_SERVER['user'] = isset($sql_server['user']) ? $sql_server['user'] : $this->SQL_SERVER['user'];
			$this->SQL_SERVER['password'] = isset($sql_server['password']) ? $sql_server['password'] : $this->SQL_SERVER['password'];
			$this->_connectDb = $this->connectDb();
		}
	}
	private function __clone(){}
	
	public static function getInstance($sql_server = Array()) {
		if( ! (self::$_instance instanceof self)){
			self::$_instance = new self();
		}
		self::$_instance->resetServer($sql_server);
		return self::$_instance;
	}
	public function getConnDB()
	{
		return $this->_connectDb;
	}
	private function connectDb()
	{
		$dbh = false;
		try{                                       //stateanalysis
			$dbh = new PDO("mysql:host={$this->SQL_SERVER['host']};port={$this->SQL_SERVER['port']};dbname={$this->SQL_SERVER['dbname']}", $this->SQL_SERVER['user'], $this->SQL_SERVER['password']);  
			
			//$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); //不显示错误
			//$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//显示警告错误，并继续执行
			$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//产生致命错误，PDOException
			$dbh->exec('set names utf8');
		}catch (Exception $e)
		{
			echo $e->getMessage();
		}
		return $dbh;
	}
	/*private $_connectDb;
	private $SQL_SERVER = Array('host'=>'127.0.0.1', 
								'port'=>'3306', 
								'dbname'=>'mysql', 
								'user'=>'root', 
								'password'=>'kaida');
	public function __construct($sql_server = Array())
	{
		$this->SQL_SERVER['host'] = isset($sql_server['host']) ? $sql_server['host'] : $this->SQL_SERVER['host'];
		$this->SQL_SERVER['port'] = isset($sql_server['port']) ? $sql_server['port'] : $this->SQL_SERVER['port'];
		$this->SQL_SERVER['dbname'] = isset($sql_server['dbname']) ? $sql_server['dbname'] : $this->SQL_SERVER['dbname'];
		$this->SQL_SERVER['user'] = isset($sql_server['user']) ? $sql_server['user'] : $this->SQL_SERVER['user'];
		$this->SQL_SERVER['password'] = isset($sql_server['password']) ? $sql_server['password'] : $this->SQL_SERVER['password'];
	}
	public function resetServer($sql_server = Array())
	{
		$this->SQL_SERVER['host'] = isset($sql_server['host']) ? $sql_server['host'] : $this->SQL_SERVER['host'];
		$this->SQL_SERVER['port'] = isset($sql_server['port']) ? $sql_server['port'] : $this->SQL_SERVER['port'];
		$this->SQL_SERVER['dbname'] = isset($sql_server['dbname']) ? $sql_server['dbname'] : $this->SQL_SERVER['dbname'];
		$this->SQL_SERVER['user'] = isset($sql_server['user']) ? $sql_server['user'] : $this->SQL_SERVER['user'];
		$this->SQL_SERVER['password'] = isset($sql_server['password']) ? $sql_server['password'] : $this->SQL_SERVER['password'];
	}
	public function connectDb()
	{
		$dbh = false;
		try{                                       //stateanalysis
			$dbh = new PDO("mysql:host={$this->SQL_SERVER['host']};port={$this->SQL_SERVER['port']};dbname={$this->SQL_SERVER['dbname']}", $this->SQL_SERVER['user'], $this->SQL_SERVER['password']);  
			
			//$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); //不显示错误
			//$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//显示警告错误，并继续执行
			$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//产生致命错误，PDOException
			$dbh->exec('set names utf8');
		}catch (Exception $e)
		{
			echo $e->getMessage();
		}
		return $dbh;
	}*/
}

/*
		private $_sb;
		static $_instance;
		
		private function __construct() {
			$this->_db = pg_connect('daname=example_db');
		}
		
		private function __clone(){}
		
		public static function getInstance() {
			if( ! (self::$_instance instanceof self)){
				self::$_instance = new self();
			}
			return self::$_instance;
		}*/